package com.supreme.cacheserver.controller;

import com.supreme.commonboot.utils.AjaxResp;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping(value = "redis-list")
public class RedisListController {

    @Resource
    private RedisTemplate<String, Serializable> redisTemplate;

    @Resource
    private List<RedisTemplate<String, Serializable>> redisTemplateList;

    @GetMapping(value = "leftPushAll/{key}/{value}")
    public AjaxResp leftPushAll(@PathVariable(value = "key") String key, @PathVariable(value = "value") String... value) {
        redisTemplate.opsForList().leftPushAll(key, value);
        return AjaxResp.instance();
    }

    @GetMapping(value = "rightPushAll/{key}/{value}")
    public AjaxResp rightPushAll(@PathVariable(value = "key") String key, @PathVariable(value = "value") String... value) {
        redisTemplate.opsForList().rightPushAll(key, value);
        return AjaxResp.instance();
    }

    @GetMapping(value = "leftPop/{key}")
    public AjaxResp leftPop(@PathVariable(value = "key") String key, @RequestParam(value = "count", required = false) Long count) {
        if (Objects.isNull(count)) {
            Serializable serializable = redisTemplate.opsForList().leftPop(key);
            return AjaxResp.instance(serializable);
        } else {
            List<Serializable> serializableList = redisTemplate.opsForList().leftPop(key, count);
            return AjaxResp.instance(serializableList);
        }
    }

    @GetMapping(value = "rightPop/{key}")
    public AjaxResp rightPop(@PathVariable(value = "key") String key, @RequestParam(value = "count", required = false) Long count) {
        if (Objects.isNull(count)) {
            Serializable serializable = redisTemplate.opsForList().rightPop(key);
            return AjaxResp.instance(serializable);
        } else {
            List<Serializable> serializableList = redisTemplate.opsForList().rightPop(key, count);
            return AjaxResp.instance(serializableList);
        }
    }

    @GetMapping(value = "range/{key}")
    public AjaxResp range(@PathVariable(value = "key") String key,
                          @RequestParam(value = "start", defaultValue = "0") long start,
                          @RequestParam(value = "end", defaultValue = "-1") long end) {
        final List<Serializable> range = redisTemplate.opsForList().range(key, 0, -1);
        return AjaxResp.instance(range);
    }

    @GetMapping(value = "index/{key}/{listIndex}")
    public AjaxResp index(@PathVariable(value = "key") String key, @PathVariable(value = "listIndex") Long listIndex) {
        final Serializable serializable = redisTemplate.opsForList().index(key, listIndex);
        return AjaxResp.instance(serializable);
    }

    @GetMapping(value = "remove/{key}/{count}/{value}")
    public AjaxResp remove(@PathVariable(value = "key") String key,
                          @PathVariable(value = "count") long count,
                          @PathVariable(value = "value") Object value) {
        Long remove = redisTemplate.opsForList().remove(key, count, value);
        return AjaxResp.instance(remove);
    }

    @GetMapping(value = "set/{key}/{index}/{value}")
    public AjaxResp set(@PathVariable(value = "key") String key,
                        @PathVariable(value = "index") Long index,
                        @PathVariable(value = "value") String value) {
        redisTemplate.opsForList().set(key, index, value);
        return AjaxResp.instance();
    }

    @GetMapping(value = "size/{key}")
    public AjaxResp size(@PathVariable(value = "key") String key) {
        Long size = redisTemplate.opsForList().size(key);
        return AjaxResp.instance(size);
    }

    @GetMapping(value = "trim/{key}/{start}/{end}")
    public AjaxResp trim(@PathVariable(value = "key") String key,
                        @PathVariable(value = "start") long start,
                        @PathVariable(value = "end") long end) {
        redisTemplate.opsForList().trim(key, start, end);
        return AjaxResp.instance();
    }

    @GetMapping(value = "rightPopAndLeftPush/{srcKey}/{desKey}")
    public AjaxResp rightPopAndLeftPush(@PathVariable(value = "srcKey") String srcKey, @PathVariable(value = "desKey") String desKey) {
        Serializable serializable = redisTemplate.opsForList().rightPopAndLeftPush(srcKey, desKey);
        return AjaxResp.instance(serializable);
    }
}
